<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\api\controller;

use app\api\lists\CardLists;
use app\api\logic\CardLogic;
use app\api\logic\RechargeLogic;
use app\api\validate\CardValidate;
use app\api\validate\RechargeValidate;
use app\common\enum\PayEnum;
use app\common\enum\user\AccountLogEnum;
use app\common\logic\AccountLogLogic;
use app\common\model\recharge\RechargeOrder;
use app\common\model\user\User;
use app\common\model\user\UserGroup;
use think\facade\Config;


/**
 * card控制器
 * Class CardGroupController
 * @package app\api\controller
 */
class CardController extends BaseApiController
{


    /**
     * @notes 获取card列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function lists()
    {
        if (isset($_GET['export']) && $_GET['export'] == 2) {
            if (!isset($_GET['secure_password'])) {
                return $this->fail('交易密码不能为空');
            }
            $user = User::findOrEmpty($this->userInfo['user_id']);
            if ($user->isEmpty()) {
                return $this->fail('用户不存在');
            }
            $passwordSalt = Config::get('project.unique_identification');
            $oldPassword = create_password($_GET['secure_password'], $passwordSalt);
            if ($oldPassword != $user['secure_password']) {
                return $this->fail('交易密码不正确');
            }
        }
        return $this->dataLists(new CardLists());
    }


    /**
     * @notes 添加card
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function add()
    {
        $params = (new CardValidate())->post()->goCheck('add');
        $params['user_info'] = $this->userInfo;
        if (!isset($params['user_id']) || $params['user_id'] == 0 || $params['user_id'] == '') {
            $params['user_id'] = $this->userInfo['user_id'];
        }
        $result = CardLogic::add($params);
        if (true === $result) {
            return $this->success('开卡成功', [], 1, 1);
        }

        return $this->fail(CardLogic::getError());
    }


    /**
     * @notes 编辑card
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function edit()
    {
        $params = (new CardValidate())->post()->goCheck('edit');
        $params['group_id'] = $this->userInfo['group_id'];
        $params['user_info'] = $this->userInfo;
        $result = CardLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(CardLogic::getError());
    }


//    /**
//     * @notes 删除card
//     * @return \think\response\Json
//     * @author likeadmin
//     * @date 2025/04/09 19:15
//     */
//    public function delete()
//    {
//        $params = (new CardValidate())->post()->goCheck('delete');
//        $params['group_id'] = $this->userInfo['group_id'];
//        $result = CardLogic::delete($params);
//        if (true === $result) {
//            return $this->success('删除成功', [], 1, 1);
//        }
//        return $this->fail(CardLogic::getError());
//    }

    public function change_user()
    {
        $params = (new CardValidate())->post()->goCheck('change');
        $params['group_id'] = $this->userInfo['group_id'];
        $result = CardLogic::change($params);
        if (true === $result) {
            return $this->success('修改成功', [], 1, 1);
        }
        return $this->fail(CardLogic::getError());
    }


    public function recharge()
    {
        $params = (new RechargeValidate())->post()->goCheck('recharge', [
            'user_id' => $this->userId,
            'terminal' => $this->userInfo['terminal'],
        ]);
        $params['user_info'] = $this->userInfo;

        // 增加用户累计充值金额及用户余额
        $result = CardLogic::recharge($params);
        if (true !== $result) {
            return $this->fail(CardLogic::getError());
        }

        return $this->success('充值成功', [], 1, 1);
    }

    /**
     * @notes
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function secure()
    {
        $params = (new CardValidate())->post()->goCheck('secure');
        $params['group_id'] = $this->userInfo['group_id'];
        $params['user_id'] = $this->userId;
        $result = CardLogic::secure($params);

        if (false === $result) {
            return $this->fail(CardLogic::getError());
        }
        return $this->success('SUCCESS', $result, 1, 1);
    }

    public function sync()
    {
        $params['group_id'] = $this->userInfo['group_id'];
        $result = CardLogic::sync($params);

        if (false === $result) {
            return $this->fail(CardLogic::getError());
        }
        return $this->success('同步成功', [], 1, 1);
    }

//    查询绑卡验证码
    public function code()
    {
        set_time_limit(31);

        $params = (new CardValidate())->post()->goCheck('code');

        $result = CardLogic::code($params);

        if (false === $result) {
            return $this->fail('FAIL');
        }
        return $this->success('SUCCESS', $result, 1, 1);
    }

    /**
     * @notes 卡片使用率
     * @return \think\response\Json
     */
    public function utilization()
    {
        $params = (new CardValidate())->post()->goCheck('utilization');
        $params['group_id'] = $this->userInfo['group_id'];
        $result = CardLogic::utilization($params);

        if (false === $result) {
            return $this->fail(CardLogic::getError());
        }
        return $this->success('SUCCESS', $result, 1, 1);
    }

    /**
     * @notes 卡片
     * @return \think\response\Json
     */
    public function task()
    {
        $params['group_id'] = $this->userInfo['group_id'];
        $result = CardLogic::task($params);
        return $this->success('SUCCESS', ['count' => $result], 1, 1);
    }

    /**
     * 锁卡
     * @return void
     */
    public function lock(){

    }

    /**
     * 绑卡
     * @return void
     */
    public function bind(){

    }

}